const { Model, DataTypes } = require("sequelize");
const sequelize = require("../app/database");
const Config = require("./config.model");

class Questionnaire extends Model {}

Questionnaire.init(
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    title: {
      type: DataTypes.STRING,
    },
    name: {
      type: DataTypes.STRING,
    },
    type: {
      type: DataTypes.INTEGER,
    },
    lv: {
      type: DataTypes.INTEGER,
    },
    link_id: {
      type: DataTypes.INTEGER,
      references: {
        model: Questionnaire,
        key: "id",
      },
    },
    config_id: {
      type: DataTypes.INTEGER,
      references: {
        model: Config,
        key: "id",
      },
    },
    options: {
      type: DataTypes.STRING,
    },
    content: {
      type: DataTypes.STRING,
    },
    explain: {
      type: DataTypes.STRING,
    },
    createAt: {
      type: DataTypes.DATE,
    },
    updateAt: {
      type: DataTypes.DATE,
    },
  },
  {
    tableName: "questionnaire",
    createdAt: "createAt",
    updatedAt: "updateAt",
    timestamps: true,
    sequelize,
  }
);

// 一对多
Config.hasMany(Questionnaire, {
  foreignKey: "config_id",
});
Questionnaire.belongsTo(Config, {
  foreignKey: "config_id",
});

Questionnaire.belongsTo(Questionnaire, {
  foreignKey: "link_id",
});

module.exports = Questionnaire;
